<template>
  <div class="post-detail">
    <!-- 头部 -->
    <div class="header">
      <div class="left" @click="$router.push('/')">
        <i class="iconfont iconjiantou2"></i>
        <i class="iconfont iconnew"></i>
      </div>
      <div class="right">
        <div class="btn-followed" v-if="post.has_follow" @click="unfollow">已关注</div>
        <div class="btn-follow" v-else @click="follow">关注</div>
      </div>
    </div>

    <!-- 主体 -->
    <div class="main">
      <div class="post-title">{{ post.title }}</div>
      <div class="post-info">
        <span>{{ post.user.nickname }}</span>
        <span>{{ post.create_date | time }}</span>
      </div>
      <div class="video" v-if="post.type === 2">
        <video :src="post.content" controls></video>
      </div>
      <!-- 新闻内容 -->
      <div class="content" v-else v-html="post.content"></div>
    </div>

    <!-- 按钮 -->
    <div class="btns">
      <div class="good" @click="like" :class="{ like: post.has_like }">
        <i class="iconfont icondianzan"></i>
        <span>{{ post.like_length }}</span>
      </div>
      <div class="share">
        <i class="iconfont iconweixin"></i>
        <span>微信</span>
      </div>
    </div>

    <!-- 评论 -->
    <div class="post-comment">
      <div class="title">精彩跟帖</div>
      <p class="no-comment" v-if="commentList.length ===0">快来抢沙发</p>
      <hm-comment @reply="reply" v-else v-for="item in commentList" :key="item.id" :comment="item"></hm-comment>
    </div>
    <!-- 底部 -->
    <div class="footer">
      <div class="comment-input" v-if="!isFocus">

      <input @focus="handleFocus" type="text" placeholder="写跟帖">

      <div class="icon-comment">
        <i class="iconfont iconpinglun-"></i>
        <span class="num">12</span>
      </div>
      <!-- 实底 -->
      <van-icon @click="star" name="star" class="star" v-if="post.has_star"/>

       <!-- 线框风格 -->
        <van-icon @click="star" name="star-o" v-else />

      <i class="iconfont iconfenxiang"></i>
      </div>
      <div class="comment-textarea" v-else>

      <textarea v-model="content" v-focus @blur="handleBlur" name="" id="" rows="3" :placeholder="placeholder"></textarea>
      <div class="send" @click="send">发送</div>
      </div>
    </div>
  </div>
</template>

<script>
import HmComment from 'components/hm-comment.vue'
export default {
  data () {
    return {
      post: {
        user: {}
      },
      isFocus: false,
      commentList: [],
      content: '',
      placeholder: '发表评论',
      parentId: ''
    }
  },
  components: {
    HmComment
  },
  async created () {
    this.getPostDetail()
    this.getCommentList()
  },
  methods: {
    async getPostDetail () {
      const id = this.$route.params.id
      const res = await this.$axios.get(`/post/${id}`)
      const { statusCode, data } = res.data
      if (statusCode === 200) {
        this.post = data
        console.log(this.post)
      }
    },
    async getCommentList () {
      const id = this.$route.params.id
      const res = await this.$axios.get(`/post_comment/${id}`)
      const { statusCode, data } = res.data
      if (statusCode === 200) {
        this.commentList = data
        console.log(this.commentList)
      }
    },
    async follow () {
      // 关注功能, 需要拿到被关注用户的 id
      const id = this.post.user.id
      // 根据 id 发送关注的 ajax 请求, 完成关注功能
      const res = await this.$axios.get(`/user_follows/${id}`)
      const { statusCode } = res.data
      if (statusCode === 200) {
        // 添加提示
        this.$toast.success('关注成功')
        // 重新渲染
        this.getPostDetail()
      }
    },
    async unfollow () {
      // 取消关注功能, 需要拿到被关注用户的 id
      const id = this.post.user.id
      // 根据 id 发送 ajax 请求, 完成取消关注功能
      const res = await this.$axios.get(`/user_unfollow/${id}`)
      const { statusCode } = res.data
      if (statusCode === 200) {
        // 添加提示
        this.$toast.success('取消关注成功')
        // 重新渲染
        this.getPostDetail()
      }
    },
    async like () {
      // 发送请求, 先获取到点赞文章的 id
      const id = this.$route.params.id
      const res = await this.$axios.get(`/post_like/${id}`)
      const { statusCode, message } = res.data
      if (statusCode === 200) {
        // console.log(res)
        this.$toast.success(message)
        this.getPostDetail()
      }
    },
    async star () {
      const id = this.$route.params.id
      const res = await this.$axios.get(`/post_star/${id}`)
      const { statusCode, message } = res.data
      if (statusCode === 200) {
        this.$toast.success(message)
        this.getPostDetail()
      }
    },
    handleFocus () {
      this.isFocus = true
    },
    handleBlur () {
      if (this.content) return
      this.isFocus = false
    },
    async send () {
      if (this.content.trim() === '') return
      const id = this.$route.params.id
      const res = await this.$axios.post(`/post_comment/${id}`, {
        content: this.content,
        parent_id: this.parentId
      })
      const { statusCode } = res.data
      if (statusCode === 200) {
        this.$toast.success('评论成功')
        this.content = ''
        this.isFocus = false
        this.placeholder = '发布评论'
        this.parentId = ''
        this.getCommentList()
      }
    },
    reply (id, nickname) {
      console.log(id, nickname)
      this.isFocus = true
      this.placeholder = `回复：@${nickname}`
      this.parentId = id
    }
  },
  watch: {
    $route () {
      // 监听当前路由的变化, 只要地址变化了, 就重新获取数据
      this.getPostDetail()
      this.getCommentList()
    }
  }
}
</script>

<style lang="scss" scoped>
.post-detail {
  padding-bottom: 100px;

  .header {
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    .left {
      i {
        vertical-align: middle;
      }
      .iconnew {
        font-size: 50px;
      }
    }
    .right {
      .btn-followed,
      .btn-follow {
        border: 1px solid #ccc;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-radius: 15px;
        width: 70px;
      }
      .btn-follow {
        background-color: red;
        color: #fff;
      }
    }
  }

  // 详情内容
  .main {
    padding: 10px;
    .post-title {
      font-size: 18px;
      font-weight: 700;
    }
    .post-info {
      color: #888;
      padding: 10px 0;
      span:first-child {
        margin-right: 10px;
      }
    }

    video {
      width: 100%;
    }
  }

  // 按钮
  .btns {
    display: flex;
    justify-content: space-around;
    padding: 20px 0;
    border-bottom: 3px solid #ccc;
    .good,
    .share {
      height: 30px;
      line-height: 30px;
      padding: 0 15px;
      border-radius: 15px;
      border: 1px solid #ccc;
      .iconfont {
        font-size: 12px;
        margin-right: 5px;
      }
      .iconweixin {
        color: green;
      }
    }

    .like {
      background-color: red;
      color: #fff;
    }
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    padding: 10px 0;
    background-color: #f7f7f7;
    border-top: 1px solid #ccc;
    .comment-input {
      display: flex;
      justify-content: space-around;
      input {
        width: 180px;
        height: 30px;
        background-color: #ddd;
        border-radius: 15px;
        padding: 0 20px;
      }
      .iconfont {
        font-size: 20px;
      }

      .icon-comment {
        position: relative;
        .num {
          position: absolute;
          background-color: red;
          right: -18px;
          top: -6px;
          height: 18px;
          line-height: 18px;
          padding: 0 10px;
          color: #fff;
          border-radius: 10px;
        }
      }
      .van-icon {
        font-size: 20px;
        line-height: 28px;
        margin-left: 5px;
        &.star {
          color: red;
        }
      }
    }

    .comment-textarea {
      display: flex;
      justify-content: space-around;
      // 让内容在一列中居底部显示
      align-items: flex-end;
      textarea {
        background-color: #ddd;
        width: 260px;
        height: 90px;
        border-radius: 10px;
        padding: 10px;
      }
      .send {
        width: 60px;
        height: 26px;
        line-height: 26px;
        text-align: center;
        background-color: red;
        color: #fff;
        border-radius: 13px;
      }
    }
  }
}
</style>
